import matplotlib.pyplot as plt
import numpy as np
import csv
from datetime import datetime
from time import strftime
from scipy.stats import scoreatpercentile

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list_int(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_data_list_float(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_data_list_exp_float(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        #for item in ra.fieldnames:
        temp = float(record[ra.fieldnames[-1]])
        sKey.append(temp)
    sKey = np.array(sKey)
    #sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def cal_dist(x1, y1, x2, y2):
    temp_dist = (float(x1 - x2))**2 + (float((y1 - y2))**2)
    #print x1, y1, x2, y2, (float(x1 - x2))**2, (float((y1 - y2))**2), temp_dist
    temp_dist = temp_dist**0.5
    #print temp_dist

    return temp_dist

def fivenum(v):
    """Returns Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input vector, a list or array of numbers based on 1.5 times the interquartile distance"""
    try:
        np.sum(v)
    except TypeError:
        print('Error: you must provide a list or array of only numbers')
    q1 = scoreatpercentile(v,25)
    q3 = scoreatpercentile(v,75)
    md = np.median(v)
    return np.min(v), q1, md, q3, np.max(v),

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration_census_2000/'
    dataCSV = filepath + '7-18-2012/census_county_migration_format_dist_exp.csv'
    data = build_data_list_float(dataCSV)

    #expCSV = filepath + '7-18-2012/census_state_gravity_piecewiseR_flow_results.csv'
    #expflow = build_data_list_exp_float(expCSV)
    
    #print centroid
    inoutflow = build_data_list_int(filepath + 'census_county_inoutflow.csv')

    hist_bin = np.zeros((100,2))
    hist_bin[:, 0] = range(1,101)
    
    i = 0
    count = np.zeros((100,2))
    for item in data:
        if item[-1] > 400000:
            print item
        temp = int(item[0]/50000)
        hist_bin[temp, 1] += (item[-1] - item[-2])
        if item[-1] - item[-2] < 0:
            count[temp, 1] += 1
        else:
            count[temp, 0] += 1
        i += 1
    #print hist_bin[:, 1]
    diff = data[:,-1] - data[:,-2]

    print fivenum(diff)
    print scoreatpercentile(diff, 1), scoreatpercentile(diff, 5), scoreatpercentile(diff, 95), scoreatpercentile(diff, 99)

    #n, bins, patches = plt.hist(diff, 50, range = [-50,210], facecolor = 'green', alpha = 0.75)
    #n, bins, patches = plt.hist(diff, 50, facecolor = 'green', alpha = 0.75)
    #plt.plot(hist_bin[:, 0] * 50, hist_bin[:, 1], 'g')
    #plt.scatter(data[:, 0], expflow, color ='b')
    #plt.loglog(hist_bin[:, 0] * 50, hist_bin[:, 1], marker = 'o', linestyle = '', ms=4, color ='b')
    #plt.loglog(hist_bin[:, 0] * 50, hist_bin[:, 1], 'b', linestyle ='--', linewidth=2)

    #plt.xlabel('distance (km)')
    #plt.ylabel('expected - observed')
    #plt.show()


    #np.savetxt(filepath + 'tempcount.csv', count, delimiter=',', fmt = '%s')